/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "pauseinstructionlogic.h"
#include "GeneralDefine.h"
#include "programengine.h"
#include "messagelog.h"

PauseInstructionLogic::PauseInstructionLogic(int robotIdIn,ProgramSet* programSetIn,
                                             RobotIo* ioControllerIn,ProgramEngine* programEngineIn)
  {
      programSet=programSetIn;
      robotId=robotIdIn;
      ioController=ioControllerIn;
      programEngine=programEngineIn;
  }


E_PROGRAM_LOOP_RESULT PauseInstructionLogic::startPause(int programNum, PauseInstruction pauseInstruct)
{
    bool pauseFlag = (*programSet)[programNum].pauseFlag;

    qDebug() << __FUNCTION__<<"======"<<__LINE__<<"===pauseFlag==="<<pauseFlag
             <<"====pcFlagValue==="<<programEngine->getPcFlagValue()<<endl;
    E_PROGRAM_LOOP_RESULT endFlag = E_PROGRAM_LOOP_RESULT_NORMAL;
    if(false == pauseFlag)
    {
        qDebug()<<"PauseInstructionLogic::startPause, paused!";
        addMsg(ENUM_MSG_REMIND,"ProgramEngine","ProgramEngine",3620,robotId
               ,(*programSet)[programNum].runningLineInt+1,0,0,0,programEngine->getCurrentProgramName());
        programEngine->ceaseProgram();
        (*programSet)[programNum].pauseFlag = true;
    }
    else
    {
        (*programSet)[programNum].pauseFlag = false;
        programEngine->calculateNextLineToRun(programNum);
    }
    endFlag = programEngine->isTheLastInstruction(programNum);
    return endFlag;
}


void PauseInstructionLogic::addMsg(int messageLevel, string componentName, string messageType,
                           int messageCode, int robotId,
                           int parameter1, int parameter2, int parameter3, int parameter4,QString message)
{
    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;


    QString infomationStr;

    switch(messageCode)
    {

    case 3620:
    {
        infomationStr =  QObject::tr("机器人:")  + QString::number(robotId);
        infomationStr = infomationStr +
                QObject::tr("; 程序(%2)暂停指令(行号%1)暂停了任务运行！").arg(parameter1)
                .arg(message);
        break;

    }



    default:
    {

    }
    }


    tmpMsg.MessageInformation = infomationStr.toStdString();

    // qDebug()<<"53444444444444444444444"<< tmpMsg.messageCode;
    MessageLog::getInstance()->addMessage(tmpMsg);
}
